#include "sqlite.h"
#include "ui_sqlite.h"

sqlite::sqlite(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::sqlite)
{
    ui->setupUi(this);
    QDateTime currentDateTime = QDateTime::currentDateTime();
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"编号"<<"时间"<<"温度"<<"湿度"<<"粉尘密度"<<"磁场强度");
    ui->dateTimeEdit_2->setMaximumDateTime(currentDateTime);
    ui->dateTimeEdit_2->setMaximumDateTime(currentDateTime);
    ui->comboBox_2->insertItems(0,QStringList()<<"temperature"<<"humidity"<<"dust_density"<<"MFS");
}

sqlite::~sqlite()
{
    delete ui;
}

void sqlite::on_pushButton_2_clicked()
{
    if(sql.open("data") == false)
    {
        qDebug()<<"打开数据库失败"<<Qt::endl;
    }
    QString str = "SELECT * FROM data;";
    QSqlQuery * pQuery;
    pQuery = sql.exec(str);
    UpData(pQuery);
    sql.close();
}

void sqlite::UpData(QSqlQuery * pQuery)
{
    QTableWidgetItem * id;
    QTableWidgetItem * time;
    QTableWidgetItem * temperature;
    QTableWidgetItem * humidity;
    QTableWidgetItem * dust_density;
    QTableWidgetItem * MFS;
    int i = 0;
    while(pQuery->next())
    {
        id= new QTableWidgetItem(pQuery->value(0).toString());
        time= new QTableWidgetItem(pQuery->value(1).toString());
        temperature= new QTableWidgetItem(pQuery->value(2).toString());
        humidity= new QTableWidgetItem(pQuery->value(3).toString());
        dust_density= new QTableWidgetItem(pQuery->value(4).toString());
        MFS= new QTableWidgetItem(pQuery->value(5).toString());
        ui->tableWidget->setItem(i,0,id);
        ui->tableWidget->setItem(i,1,time);
        ui->tableWidget->setItem(i,2,temperature);
        ui->tableWidget->setItem(i,3,humidity);
        ui->tableWidget->setItem(i,4,dust_density);
        ui->tableWidget->setItem(i,5,MFS);
        i++;
    }
}

void sqlite::on_pushButton_4_clicked()
{
    QString text = ui->dateTimeEdit->text();
    QString text2 = ui->dateTimeEdit_2->text();
    if(sql.open("data") == false)
    {
        qDebug()<<"打开数据库失败"<<Qt::endl;
    }
    QString str = "SELECT * FROM data "
                  "WHERE time BETWEEN '%1' AND '%2';";
    QSqlQuery * pQuery;
    pQuery = sql.exec(str.arg(text).arg(text2));
    qDebug()<<str.arg(text).arg(text2);
    UpData(pQuery);
    sql.close();
}


void sqlite::on_pushButton_5_clicked()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    ui->dateTimeEdit_2->setDateTime(currentDateTime);
}


void sqlite::on_pushButton_6_clicked()
{
    QString text = ui->comboBox_2->currentText();
    QString text1 = ui->doubleSpinBox->text();
    QString text2 = ui->doubleSpinBox_2->text();
    if(sql.open("data") == false)
    {
        qDebug()<<"打开数据库失败"<<Qt::endl;
    }
    QString str = "SELECT * FROM data "
                  "WHERE %1 BETWEEN '%2' AND '%3';";
    QSqlQuery * pQuery;
    pQuery = sql.exec(str.arg(text).arg(text1).arg(text2));
    UpData(pQuery);
    qDebug()<<str.arg(text).arg(text1).arg(text2);
    sql.close();
}


void sqlite::on_pushButton_3_clicked()
{
    ui->tableWidget->clearContents();
}


void sqlite::on_pushButton_clicked()
{
    this->close();
}

